package com.itlaobing.instance;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;

/* 
 一个 @WebServlet 注解 等同于在 web.xml 中添加了一下配置:
 	<servlet>
		<servlet-name>tiger</servlet-name>
		<servlet-class>com.itlaobing.instance.TigerServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>tiger</servlet-name>
		<url-pattern>/tiger/*</url-pattern>
	</servlet-mapping>
 */

// 如果在 @WebServlet 没有显式指定 name 属性则该属性取值是 该Servlet类的规范化类名 (简答理解为 包名.类名 )
@WebServlet( name = "tiger" , urlPatterns = "/tiger/*" )
public class TigerServlet extends GenericServlet {

	private static final long serialVersionUID = -4752413064018728169L;

	@Override
	public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
		// 在服务器端的终端(Console)中输出
		this.log( "Servlet【" + this.toString() + "】");
		this.log( "ServletRequest【" + req.toString() + "】");
		this.log( "ServletResponse【" + resp.toString() + "】");
		
		String servletName = this.getServletName(); // 这是从GenericServlet继承来的方法(本质上是ServletConfig接口中声明的方法)
		
		// 通过响应对象设置向客户端(比如浏览器)发送的内容类型(mime-type)，并指定内容的编码
		resp.setContentType( "text/html;charset=UTF-8" );
		
		// 获得可以向响应对象中输出数据的字符打印流
		PrintWriter out = resp.getWriter();
		out.println( "<div style='text-align:center;font-weight:bolder;'>" );
		out.println( servletName );
		out.println( "</div>" );
	}

}
